
import { MessageBox, Message } from 'element-ui'
/**
 *
 * @param {string} text 确认框文本内容
 * @param {string} title 确认框标题提醒文本
 * @param {object} model api实例化对象
 * @param {string} modelAction 要调用的model对应方法名称
 * @param {object} data model方法需要传递的参数
 * @param {string} successText 操作成功的提示文本
 */
const useConfirm = ({
    text = '是否确定删除表单',
    title = '确认提示',
    model,
    modelAction = '',
    data = {},
    successText = ''
}) => {
    return new Promise((resolve, reject) => {
        MessageBox.confirm(text, title, {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning',
            beforeClose: async(action, instance, done) => {
                if (action === 'confirm') {
                    if (!model) {
                        done()
                        resolve()
                        return
                    }
                    try {
                        instance.confirmButtonLoading = true
                        // instance.confirmButtonText = '...'
                        await model[modelAction](data)
                        successText &&
                            Message({
                                type: 'success',
                                message: successText
                            })
                        done()
                        resolve()
                    } finally {
                        done()
                        reject()
                        instance.confirmButtonLoading = false
                    }
                } else {
                    done()
                }
            }
        })
    })
}
export default useConfirm
